package com.cosmic.pizza.service.Impl;

import com.cosmic.pizza.dao.AddressDao;
import com.cosmic.pizza.dao.Impl.AddressDaoImpl;
import com.cosmic.pizza.pojo.Address;
import com.cosmic.pizza.pojo.PageBean;
import com.cosmic.pizza.service.AddressService;
import com.cosmic.pizza.util.JedisUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import java.util.*;

public class AddressServiceImpl implements AddressService {
    private AddressDao addressDao = new AddressDaoImpl();
    @Override
    public PageBean<Address> findAddress(Integer uid, int pageSize, int currentPage) {
        PageBean<Address> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);

        int start = (currentPage - 1) * pageSize;
        List<Address> list = addressDao.findAddressList(uid, start, pageSize);
        pageBean.setList(list);

        int count = addressDao.findCountByUid(uid);
        pageBean.setTotalCount(count);

        int totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }

    @Override
    public List<Address> findAddress(Integer uid) {
        return addressDao.findAddressList(uid);
    }

    @Override
    public boolean delete(int add_id) {
        int i = addressDao.deleteByAid(add_id);
        return i > 0;
    }

    @Override
    public boolean setDefault(int add_id) {
        int i = addressDao.setDefaultByAid(add_id);
        return i > 0;
    }

    @Override
    public boolean addAddress(Integer uid, Address address) {
        int i = addressDao.insertAddress(uid, address);
        return i > 0;
    }

    @Override
    public Map<String, String> findAddressAndNameById(Integer add_id) {
        int _add_id = add_id;
        String address = addressDao.findAddressById(_add_id);
        String name = addressDao.findNameById(_add_id);
        Map<String,String> map = new HashMap<>();
        map.put("address",address);
        map.put("name",name);
        return map;
    }

    @Override
    public Address findFirstAddress(Integer uid) {
        return addressDao.findDefaultAddressByUid(uid);
    }

    @Override
    public Address findAddressByaid(Integer add_id) {
        return addressDao.findAddressByAid(add_id);
    }

    @Override
    public boolean update(Address address) {
        int i = addressDao.updateAddress(address);
        return i > 0;
    }
}
